import CryptoJS from "crypto-js";

const SECRET_KEY = CryptoJS.enc.Utf8.parse("testkey123456789");
const SECRET_IV = CryptoJS.enc.Utf8.parse('testiv1234567890');
/**
 * 加密
 * @param data 需要加密的文本
 */
export function encrypt(data: string) {
    const dataHex = CryptoJS.enc.Utf8.parse(data);
    const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, {
        iv: SECRET_IV,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
  return encrypted.ciphertext.toString();
}

/**
 * 
 * @param data 解密
 * @returns 
 */
export function decrypt(data: string) {
    const encryptedHexStr = CryptoJS.enc.Hex.parse(data);
    const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    const decrypt = CryptoJS.AES.decrypt(str, SECRET_KEY, {
        iv: SECRET_IV,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}